/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#include "settingsdialog.h"
#include "ui_settingsdialog.h"

#include <QtCore/QSettings>

using namespace greenhills::client;

CSettingsDialog::CSettingsDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::CSettingsDialog)
{
    m_ui->setupUi(this);

#if defined Q_OS_LINUX
    for(int i=1;i<=10;i++)
        m_ui->cbGpsDevice->addItem(
                QString("//dev/rfcomm%1").arg(i));
#elif defined Q_OS_WIN32
    for(int i=1;i<=20;i++)
        m_ui->cbGpsDevice->addItem(
                QString("\\\\.\\COM%1").arg(i));
#elif defined Q_OS_WINCE
    for(int i=1;i<=10;i++)
        m_ui->cbGpsDevice->addItem(
                QString("COM%1:").arg(i));
#endif

    QSettings settings("Tieto", "GreenHills", this);
    settings.beginGroup("gps");
    m_ui->cbGpsDevice->setEditText(settings.value("device", "").toString());
    settings.endGroup();

    settings.beginGroup("server");
    m_ui->leHostname->setText(settings.value("host", "").toString());
    settings.endGroup();
}

CSettingsDialog::~CSettingsDialog()
{
    delete m_ui;
}

void CSettingsDialog::accept()
{
    QSettings settings("Tieto", "GreenHills", this);
    settings.beginGroup("gps");
    settings.setValue("device", m_ui->cbGpsDevice->lineEdit()->text());
    settings.endGroup();

    settings.beginGroup("server");
    settings.setValue("host", m_ui->leHostname->text());
    settings.endGroup();

    QDialog::accept();
}

void CSettingsDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
